package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.RemainReservationCountVo;
import com.zzyl.nursing.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{

    @Autowired
    private ReservationService reservationService;

    /**
     * 查询当天取消预约次数
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询当天取消预约次数")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        System.out.println("userId:---------------------------" + userId);
        Integer count =  reservationService.getCancelledReservationCount(userId);
        System.out.println("count---------------------------"+count);
        return R.ok(count);
    }

    /**
     * 查询每一个时间段内剩余预约次数:一个时间段内只能预约6个人
     */

    @GetMapping("/countByTime")
    @ApiOperation("查询每一个时间段内剩余预约次数:一个时间段内只能预约6个人")
    public R<List<RemainReservationCountVo>> getRemainCount(long time){
        List<RemainReservationCountVo> list =  reservationService.getRemainCount(time);
        return R.ok(list);
    }

    /**
     * 新增预约
     */
    @PostMapping()
    @ApiOperation("新增预约")
    public AjaxResult add( @RequestBody ReservationDto reservationDto){
        reservationService.add(reservationDto);
        return success();
    }

    /**
     * 分页查询预约信息
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R<Map<String, Object>> page(/*@RequestParam (defaultValue = "0") int status*/){
        startPage();
        //List<ReservationVo> list = reservationService.selectByPage(status);
        List<Reservation> list = reservationService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("total", list.size());

        return R.ok(map);
    }

    /**
     * 取消预约:根据id修改状态为：2
     */

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel( @PathVariable long id){
        reservationService.cancel(id);
        return success();
    }
}